package websimulator;

import java.io.IOException;
import java.io.OutputStream;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.catalina.Session;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;

import buildings.House;

import readwrite.Connector;

import calculations.Graph;
import calculations.GraphModul;
import calculations.LevelOfIllumination;
import calculations.Moduls;
import calculations.Temperatur;

/**
 * Servlet implementation class ModuleSimpleSimulator
 */
@WebServlet("/ModuleSimpleSimulator")
public class ModuleSimpleSimulator extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Temperatur temp;
	private LevelOfIllumination levelOfIllumination;
	private ArrayList<Integer> modulIds;
	private HttpSession session;
	private Connector con;
       
    /**
     * @throws Exception 
     * @see HttpServlet#HttpServlet()
     */
    public ModuleSimpleSimulator() throws Exception {
        super();
        temp = new Temperatur();
        con = new Connector();
    	levelOfIllumination = new LevelOfIllumination (true);
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String bigSimName = request.getParameter("name");
		session = request.getSession();
		String answer = "";
		if (bigSimName != "")
		{ 
			try {
				con.addBigSimulation(bigSimName, con.GetUserID((String)session.getAttribute("username")));
				answer += "<input type=\"hidden\" id=\"simId\" name=\"simId\" value=\""+con.getBigSimulationId(bigSimName)+"\">";
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//response.sendRedirect("BigSimulation.jsp");
			//request.getSession().removeAttribute("modd");
		}
		
		response.setContentType("text/plain");
		response.getWriter().write(answer);
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int modulId = Integer.parseInt(request.getParameter("modulId"));
		int simId = Integer.parseInt(request.getParameter("simId"));
		if (modulId != 0)
		{ 			
			try {
				con.addModulToBigSimulation(modulId, simId);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			response.setContentType("text/plain");
			response.getWriter().write("<div id=\"response\"><h3>Du har lagt til en ny modul</h3></div>");
		}
	}

}
